package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyJianPingWangLou;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 *  石家庄简平网络
 *
 *  @author WJB
 */
@ApiPlan(productId = "312", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5JianPingWangLou implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {

        return JsonUtils.toJson( MapUtils.newHashMap(newMap -> {
            newMap.put("phoneMd5", apiDataExchangeParameter.getPhoneMd5());
        }));
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 500);
        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        boolean status = Eq.object(jsonObject.getString("status"), "SUCCESS");
        if (!status) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        OrgApplyJianPingWangLou data = new OrgApplyJianPingWangLou();
        data.setChannel("OV8EdX");
        data.setName(apiApplyParameter.getUserName());
        data.setCity(apiApplyParameter.getCity());
        data.setPhone(apiApplyParameter.getPhone());
        data.setIdnum(apiApplyParameter.getIdcard());
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .end(0);
        data.setSex(sex);
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 2)
                .when(apiApplyParameter.getSesameCredit(), "2", 2)
                .when(apiApplyParameter.getSesameCredit(), "3", 3)
                .when(apiApplyParameter.getSesameCredit(), "4", 4)
                .end(2);
        data.setZhima_credit(zhima);
        //  年龄
        data.setAge(apiFieldConverter.getAgeInt());
        //  房
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                    .end(1);
            data.setHouse_property(text);
        }
        //  车
        data.setCar_property(apiFieldConverter.hasCarInfoToNumber());
        //  学历
        Integer educationAttainment = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", 0)
                .when(apiApplyParameter.getEducationAttainment(), "2", 1)
                .when(apiApplyParameter.getEducationAttainment(), "3", 2)
                .when(apiApplyParameter.getEducationAttainment(), "4", 3)
                .when(apiApplyParameter.getEducationAttainment(), "5", 4)
                .when(apiApplyParameter.getEducationAttainment(), "6", 5)
                .end();
        data.setEducation(educationAttainment);
        //  企业主
        Integer career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 0)
                .when(apiApplyParameter.getCareer(), "2", 0)
                .when(apiApplyParameter.getCareer(), "3", 1)
                .when(apiApplyParameter.getCareer(), "4", 0)
                .end();
        data.setEnterprise(career);
        //  社保
        data.setHas(apiFieldConverter.hasSocialSecurityToNumber());
        //  公积金
        data.setFund(apiFieldConverter.hasProvidentFundToNumber());
        //  保单
        data.setLife_insurance(apiFieldConverter.hasInsurancePolicyToNumber());
        //  逾期
        data.setLate(apiFieldConverter.hasOverdueToNumber());
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 500);
        if (!code) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        boolean status = Eq.object(jsonObject.getString("status"), "success");
        if (!status) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
